package com.goroom.v4;

import java.util.Random;

/**
 * 用static共享统一变量，可能导致的问题：不同的线程访问到的数据产生错乱
 */
public class ThreadLocalV1 {

    private static int number ;

    public static void main(String[] args) {
        for(int i = 0; i < 2; i ++){
            new Thread(){
                @Override
                public void run() {
                    number = (int) (Math.random() * 1000);
                    System.out.println("[" + Thread.currentThread().getName() +"] " +
                            Thread.currentThread().getId() + " generate number: " + number);
                    new A().get();
                    new B().get();
                }
            }.start();
        }
    }

    static class A{
        public void get(){
            int num = number;
            System.out.println("class A " + Thread.currentThread().getName() + ": ==>> " + num );
        }
    }

    static class B{
        public void get(){
            int num = number;
            System.out.println("class B " + Thread.currentThread().getName() + " ==>> " + num );
        }
    }
}
